package xdu.lz.stage3_classloader.chapter2_classloader;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 注意要删除当前包下的SimpleObj对象，不然会被系统加载器加载，因为系统加载器是自定义加载器的父加载器！！！！
 */
public class MyClassLoaderTest2 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

        MyClassLoader classLoader1 = new MyClassLoader("myclassLoader1");
        //这里的父子是包含关系，不是继承关系。
        MyClassLoader classLoader2 = new MyClassLoader("myclassLoader2",classLoader1);

        classLoader2.setDir("F:\\IDEA\\IDEA\\workspace\\multi-threaded-concurrency\\myclassloader2");

        // classloader2->classloader1->系统classloader-> 扩展classloader->根classloader
        //根classloader(没有)->扩展classloader(没有)->系统classloader(没有)->classloader1(找到)
        //如果classloader1也没找到，会到classloader2找，classloader2没找到则报notFind
        Class<?> clazz = classLoader2.
                loadClass("xdu.lz.stage3_classloader.chapter2_classloader.SimpleObj");

        System.out.println(clazz);

        System.out.println(((MyClassLoader)clazz.getClassLoader()).getClassLoaderName());

    }
}
